package com.wang.java.basic.framework.queue.blockingqueue;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * 链表阻塞队列
 * 数据结构：链表
 * 线程安全：是，基于ReentrantLock实现，put()和take()操作是两把锁
 * 队列长度：无界
 * 阻塞性：阻塞
 */
public class LinkedBlockingQueueTest {
    public static void main(String[] args) {
        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<>();
        // put线程
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    linkedBlockingQueue.put(i + "");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        // take()线程
        new Thread(() -> {
            while (true) {
                String message = null;
                try {
                    message = linkedBlockingQueue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (message != null) {
                    System.out.println("take：" + message);
                }
            }

        }).start();
    }
}
